/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;

import BE.Cliente;
import BE.Ocurrencia;
import BE.Usuarioventas;
import BE.Visita;
import BL.LogBL;
import BL.ModuloBL;
import BL.SeveridadBL;
import BL.TipoOperacionBL;
import BL.VisitaBL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import javax.servlet.http.HttpSession;

/**
 *
 * @author jcguzmanr
 */
public class mobListaClientes extends LookupDispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
      Map map = new HashMap();
      map.put("button.RegistrarVisita", "registrarVisita");
      map.put("button.RegistrarPedido", "registrarPedido");
      return map;
    }
    
    public ActionForward registrarVisita(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        boolean boolRegistrado=false;
        VisitaBL objVisitaBL = new VisitaBL();

        HttpSession session = request.getSession();
        session.setAttribute("IdCliente", request.getParameter("IdCliente"));
        Visita objVisitaBE = new Visita();

        //Comentario
        objVisitaBE.setCvComentario(request.getParameter("comentario"));
        //Fecha ACtual
        Calendar cal = Calendar.getInstance();
        objVisitaBE.setDateFechavisita(cal.getTime());
        //Cliente
        Cliente ClienteTemp = new Cliente();
       // ClienteTemp.setSrlIdpersona(Integer.parseInt(request.getParameter("cliente")));
        ClienteTemp.setSrlIdpersona(Integer.parseInt(request.getParameter("IdCliente")));
        objVisitaBE.setCliente(ClienteTemp);
        //Ocurrencia
        Ocurrencia OcurrenciaTemp = new Ocurrencia();
        if (!request.getParameter("clienteEsta").equals("")) {
            OcurrenciaTemp.setSrlIdocurrencia(Integer.parseInt(request.getParameter("clienteEsta")));
        } else OcurrenciaTemp.setSrlIdocurrencia(Integer.parseInt(request.getParameter("clienteNoEsta")));
        objVisitaBE.setOcurrencia(OcurrenciaTemp);
        //Usuario Ventas
        int intIdUsuario = (Integer) session.getAttribute("uid");

        Usuarioventas UsuarioventasTemp = new Usuarioventas();

        //UsuarioventasTemp.setSrlIdpersona(Integer.parseInt(request.getParameter("idusuario")));
        UsuarioventasTemp.setSrlIdpersona(intIdUsuario);
        objVisitaBE.setUsuarioventas(UsuarioventasTemp);

        boolRegistrado = objVisitaBL.insertarVisita(objVisitaBE);

        LogBL.GenerarLog(request, ModuloBL.modules.VENTASMOVILES.ordinal(), SeveridadBL.nivel.AVISO.ordinal(), TipoOperacionBL.tipo.CREAR.ordinal(), "Registrar Visita", "Se registra una visita por el movil");

        return mapping.findForward("registrarPedido");
    }

        public ActionForward registrarPedido(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

            HttpSession session = request.getSession();
            session.setAttribute("IdCliente", request.getParameter("IdCliente"));
                return mapping.findForward("registrarPedido");
        }

        public ActionForward mostrarPedidosCliente(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

            HttpSession session = request.getSession();
            session.setAttribute("IdCliente", request.getParameter("IdCliente"));
                return mapping.findForward("mostrarPedidosCliente");
        }
}